package com.jk.smartelderlycare.rabbitmq;

import com.jk.smartelderlycare.config.SMSUtils;
import com.jk.smartelderlycare.entity.TbOrder;
import com.jk.smartelderlycare.service.TbOrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @author hhk
 * @version 1.0
 * @ClassName TbOrderReceiver
 * @program: smart-elderly-care
 * @description: TODO
 * @date 2024/12/3 20:24
 */
@Component
public class TbOrderReceiver {

    @Autowired
    private TbOrderService tbOrderService;





    @RabbitListener(queues = "Tborder.message")
    public void processOrder(TbOrder tbOrder, @Headers Map<String,Object> header , Channel channel) throws IOException {
        //新增数据库
        tbOrderService.addOrder(tbOrder);

        // 生成随机验证码
//        String code = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
        //获取手机号
//        String phoneNumber = tbOrder.getOrdersContact();
        //发送短信
//        SMSUtils.sendMessage("阿里云短信测试", "SMS_154950909", "19846830209", code);

        Long deliveryTag  = (Long) header.get(AmqpHeaders.DELIVERY_TAG);
        channel.basicAck(deliveryTag,false);
        System.out.println("接收到的消息为 exchange,topic.message : " + tbOrder.toString());
    }


}
